/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example;

import jsc.datastructures.GroupedData;
import jsc.independentsamples.OneWayANOVA;
import jsc.distributions.AbstractDistribution.*;
import jsc.distributions.FishersF;

/**
 *
 * @author Nadeeshani Hewage
 * Undergraduate: University of Moratuwa, Sri Lanka
 */
public class OneWayAnova {

    String labels[];
    double observations[];
    GroupedData core;
    OneWayANOVA tester;
    FishersF distribution;
    String[][] table;
    double treatmentDOF, errorDOF, totalDOF;        // DOF stands for Degrees of Freedom
    double treatmentSS, errorSS, totalSS;           // SS stands for Sum of Squares
    double treatmentMS, errorMS;                    // MS stands for Mean Square
    double fRatio, fTheorotical;

    public OneWayAnova(String[][] treatments, double[][] responseVariable){

        labels=new String[treatments.length];               //creates and inserts cell data of treatment labels
                                                            //from the spreadsheet to the string array
        for(int i=0;i<treatments.length;i++){
            labels[i]=treatments[i][0];
        }

        observations=new double[responseVariable.length];   //creates and inserts cell data of observations
                                                            //from the spreadsheet to the string array
                for(int i=0;i<responseVariable.length;i++){
            observations[i]=responseVariable[i][0];
        }

        core=new GroupedData(observations,labels);          //GroupedData datastructure instance
        tester=new OneWayANOVA(core);                       //Anova Instance


    }

    public String[][] OneWayTable(){                        //required values to create the Analysis of variance table

        //degrees of freedom
        treatmentDOF=(tester.getTreatmentCount()-1);
        errorDOF    =(tester.getN()-tester.getTreatmentCount());
        totalDOF    =(tester.getN()-1);
        //Sums of squares: treatmentSS, errorSS, totalSS;
        treatmentSS =tester.getEss();
        errorSS     =tester.getRss();
        totalSS     =tester.getTss();
        //Mean squares: double treatmentMS, errorMS;
        treatmentMS =tester.getEms();
        errorMS     =tester.getRms();
        //fCalculated
        fRatio      =tester.getTestStatistic();
        distribution = new FishersF(treatmentDOF,errorDOF);

        String StreatmentDOF, SerrorDOF, StotalDOF;          // DOF stands for Degrees of Freedom
        String StreatmentSS, SerrorSS, StotalSS;             // SS stands for Sum of Squares
        String StreatmentMS, SerrorMS;                       // MS stands for Mean Square
        String SfRatio, statem;

        StreatmentDOF=""+treatmentDOF;                       //concatination of strings
        SerrorDOF=""+errorDOF;
        StotalDOF=""+totalDOF;
        StreatmentSS=""+treatmentSS;
        SerrorSS=""+errorSS;
        StotalSS=""+totalSS;
        StreatmentMS=""+treatmentMS;
        SerrorMS=""+errorMS;
        SfRatio=""+fRatio;
        statem= "Your data supports null hypothesis upto "+tester.getSP()+"significance level";          //TEST=""+fTheorotical;


        table=new String[][]{{"Null Hypothesis: Means of all treatments are equal"},                    //output table
                             {"Source","DOF","SS","MS","F-ratio"},
                             {"Treatment",StreatmentDOF,StreatmentSS,StreatmentMS,SfRatio},
                             {"Error",SerrorDOF,SerrorSS,SerrorMS,""},
                             {"Total",StotalDOF,StotalSS,"",""},
                             {statem}};

        return table;
    }

}
